package com.space.school.dao.impl;

import com.space.school.dao.ProductInfoDao;
import com.space.school.domain.Category;
import com.space.school.domain.ProductInfo;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class ProductInfoDaoImpl implements ProductInfoDao {
    //    获取数据库并连接
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(SpaceJDBCUtils.getDs());
    @Override
    public List<ProductInfo> search(int start, int pageSize, Map<String, String[]> map) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("select a.*,b.category_name from product_info a left join product_category b  on a.category_type=b.category_type  where 1=1  ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
//        取map中的键的数组
        Set<String> keyset=map.keySet();
        for (String key:keyset) {
            if(!map.get(key)[0].equals("") && !key.equals("currentPage")){
                stringBuffer.append(" and " + key + " like ?");
                params.add("%"+map.get(key)[0]+"%");
            }
        }
        params.add(start);
        params.add(pageSize);
        stringBuffer.append(" order by product_id  limit ?,? ");
        return jdbcTemplate.query(stringBuffer.toString(), new BeanPropertyRowMapper<ProductInfo>(ProductInfo.class),params.toArray());
    }

    @Override
    public int count(Map<String, String[]> map) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("select count(*) from product_info a left join product_category b  on a.category_type=b.category_type  where 1=1  ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
//        取map中的键的数组
        Set<String> keyset=map.keySet();
        for (String key:keyset) {
            if(!map.get(key)[0].equals("")&& !key.equals("currentPage")){
                stringBuffer.append(" and " + key + " like ?");
                params.add("%"+map.get(key)[0]+"%");
            }
        }
        return jdbcTemplate.queryForObject(stringBuffer.toString(),Integer.class,params.toArray());
    }

    @Override
    public void save(ProductInfo productInfo) {
        // 新增语句
        String sql="insert into product_info (product_name,product_price,product_stock,product_description,product_icon,product_status,category_type,create_time) values(?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,productInfo.getProduct_name(),productInfo.getProduct_price(),productInfo.getProduct_stock(),productInfo.getProduct_description(),productInfo.getProduct_icon(),productInfo.getProduct_status(),productInfo.getCategory_type(),new Date());
    }

    @Override
    public List<Category> findAll() {
        String sql="select * from product_category  ";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Category>(Category.class));
    }

    @Override
    public ProductInfo findById(int id) {
        String sql = "select * from product_info where product_id=?";
        List<ProductInfo> productInfo=jdbcTemplate.query(sql, new BeanPropertyRowMapper<ProductInfo>(ProductInfo.class),id);
        if(productInfo.size()>0){
            return productInfo.get(0);
        }
        return null;
    }

    @Override
    public void update(Map<String, String[]> map) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("update product_info set ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
        int id=0;
        Set<String> keyset=map.keySet();
        for (String key:keyset) {
            if(!map.get(key)[0].equals("")){
                if(key.equals("product_id")){
                    id=Integer.parseInt(map.get(key)[0]);
                }else {
                    stringBuffer.append(" " + key + " = ? ,");
                    if(key.equals("product_price")||key.equals("product_stock")||key.equals("product_status")||key.equals("category_type")){
                        params.add(Integer.parseInt(map.get(key)[0]));
                    }else {
                        params.add(" "+map.get(key)[0]+" ");
                    }
                }
            }
        }
        stringBuffer.deleteCharAt(stringBuffer.length()-1);
        stringBuffer.append(" where product_id=?");
        params.add(id);
        jdbcTemplate.update(stringBuffer.toString(),params.toArray());
    }

}
